package bb.lanxing.activity.mine

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import bb.lanxing.R
import bb.lanxing.activity.base.BaseActivity
import bb.lanxing.network.MyHttpClient
import bb.lanxing.network.NetSubscribe
import bb.lanxing.util.image.GlideUtil
import bb.lanxing.util.Log
import bb.lanxing.util.text.TextUtils
import org.json.JSONObject
import rx.Observable
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class RecommendActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.help_recommend)
        setupActionBar(true)
        val shareButton = findViewById<Button>(R.id.sendRecommend)
        shareButton.setOnClickListener {
            val editText =
                findViewById<EditText>(R.id.recommendContent) ?: return@setOnClickListener
            val intent = Intent("android.intent.action.SEND")
            intent.setType("text/plain")
            intent.putExtra(
                "android.intent.extra.SUBJECT",
                this@RecommendActivity.getString(R.string.mine_setting_recommend_dialog_subject)
            )
            intent.putExtra("android.intent.extra.TEXT", editText.text.toString())
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            val recommendActivity = this@RecommendActivity
            recommendActivity.startActivity(
                Intent.createChooser(
                    intent,
                    recommendActivity.getString(R.string.mine_setting_recommend_dialog_title)
                )
            )
        }
        requestApkUrl()
    }

    private fun requestApkUrl() {
        Observable.create(
            NetSubscribe(
                MyHttpClient.getNewestVersion()
            )
        ).subscribeOn(Schedulers.io()).observeOn(
            AndroidSchedulers.mainThread()
        ).subscribe(object : Observer<String> {
            override fun onCompleted() {
            }

            override fun onError(e: Throwable) {
            }

            override fun onNext(str: String) {
                Log.logd("RecommendActivity onNext str = $str")
                val json = JSONObject(str)
                val jsonVersion = json.getJSONObject("version")
                val downloadUrl = jsonVersion.optString("downloadUrl")
                if (!TextUtils.isEmptyOrNull(downloadUrl)) {
                    val editText = findViewById<EditText>(R.id.recommendContent)
                    editText.setText(
                        getString(
                            R.string.mine_setting_recommend_content_send, downloadUrl
                        )
                    )
                }
                val downloadQrCode = jsonVersion.optString("downloadQrCode")
                val imgQrcode = findViewById<ImageView>(R.id.img_qrcode)
                if (!TextUtils.isEmptyOrNull(downloadQrCode)) {
                    GlideUtil.getInstance().showImage(downloadQrCode, imgQrcode)
                }
            }
        })
    }
}
